from django.shortcuts import render
from django.contrib.auth import login,logout,authenticate
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.

def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse('learning_logs:index'))

def register(request):
    ''' 注册用户 '''
    if request.method != "POST":
        form =  UserCreationForm()
    else:
        form = UserCreationForm(data=request.POST)
        new_user = form.save()
        if form.is_valid():
            authenticated_user = authenticate(username=new_user.username,password=request.POST['password1'])
            login(request,authenticated_user)
            return HttpResponseRedirect(reverse("learning_logs:index"))
    return render(request,"register.html",context={"form":form})